/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataAccess;

import dataAccess.exceptions.NonexistentEntityException;
import dataAccess.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entities.Users;
import entities.Lab;
import entities.TimeTable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author QUY-THI
 */
public class TimeTableJpaController implements Serializable {

    public TimeTableJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TimeTable timeTable) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Users userID = timeTable.getUserID();
            if (userID != null) {
                userID = em.getReference(userID.getClass(), userID.getUserID());
                timeTable.setUserID(userID);
            }
            Lab labID = timeTable.getLabID();
            if (labID != null) {
                labID = em.getReference(labID.getClass(), labID.getLabID());
                timeTable.setLabID(labID);
            }
            em.persist(timeTable);
            if (userID != null) {
                userID.getTimeTableList().add(timeTable);
                userID = em.merge(userID);
            }
            if (labID != null) {
                labID.getTimeTableList().add(timeTable);
                labID = em.merge(labID);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTimeTable(timeTable.getId()) != null) {
                throw new PreexistingEntityException("TimeTable " + timeTable + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TimeTable timeTable) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TimeTable persistentTimeTable = em.find(TimeTable.class, timeTable.getId());
            Users userIDOld = persistentTimeTable.getUserID();
            Users userIDNew = timeTable.getUserID();
            Lab labIDOld = persistentTimeTable.getLabID();
            Lab labIDNew = timeTable.getLabID();
            if (userIDNew != null) {
                userIDNew = em.getReference(userIDNew.getClass(), userIDNew.getUserID());
                timeTable.setUserID(userIDNew);
            }
            if (labIDNew != null) {
                labIDNew = em.getReference(labIDNew.getClass(), labIDNew.getLabID());
                timeTable.setLabID(labIDNew);
            }
            timeTable = em.merge(timeTable);
            if (userIDOld != null && !userIDOld.equals(userIDNew)) {
                userIDOld.getTimeTableList().remove(timeTable);
                userIDOld = em.merge(userIDOld);
            }
            if (userIDNew != null && !userIDNew.equals(userIDOld)) {
                userIDNew.getTimeTableList().add(timeTable);
                userIDNew = em.merge(userIDNew);
            }
            if (labIDOld != null && !labIDOld.equals(labIDNew)) {
                labIDOld.getTimeTableList().remove(timeTable);
                labIDOld = em.merge(labIDOld);
            }
            if (labIDNew != null && !labIDNew.equals(labIDOld)) {
                labIDNew.getTimeTableList().add(timeTable);
                labIDNew = em.merge(labIDNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = timeTable.getId();
                if (findTimeTable(id) == null) {
                    throw new NonexistentEntityException("The timeTable with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TimeTable timeTable;
            try {
                timeTable = em.getReference(TimeTable.class, id);
                timeTable.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The timeTable with id " + id + " no longer exists.", enfe);
            }
            Users userID = timeTable.getUserID();
            if (userID != null) {
                userID.getTimeTableList().remove(timeTable);
                userID = em.merge(userID);
            }
            Lab labID = timeTable.getLabID();
            if (labID != null) {
                labID.getTimeTableList().remove(timeTable);
                labID = em.merge(labID);
            }
            em.remove(timeTable);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TimeTable> findTimeTableEntities() {
        return findTimeTableEntities(true, -1, -1);
    }

    public List<TimeTable> findTimeTableEntities(int maxResults, int firstResult) {
        return findTimeTableEntities(false, maxResults, firstResult);
    }

    private List<TimeTable> findTimeTableEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(TimeTable.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TimeTable findTimeTable(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TimeTable.class, id);
        } finally {
            em.close();
        }
    }

    public int getTimeTableCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<TimeTable> rt = cq.from(TimeTable.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
